package com.tarena.luban.worker.server.repo.impl;

import com.tarena.luban.worker.server.bo.WorkerCategoryBO;
import com.tarena.luban.worker.server.dao.WorkerCategoryDao;
import com.tarena.luban.worker.server.repo.WorkerCategoryRepository;
import com.tarena.luban.worker.server.repo.converter.WorkerCategoryConverter;
import com.tarena.tp.luban.worker.po.WorkerCategory;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCategoryParam;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCreateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class WorkerCategoryRepoImpl implements WorkerCategoryRepository {
    @Autowired
    private WorkerCategoryConverter workerCategoryConverter;
    @Autowired
    private WorkerCategoryDao workerCategoryDao;
    @Override
    public void saveWorkerCategory(WorkerCreateParam workerCreateParam) {
        //拿到categorylist
        List<WorkerCategoryParam> params = workerCreateParam.getWorkerCategoryParams();
        //循环封装 userId
        for (WorkerCategoryParam param : params) {
            param.setUserId(workerCreateParam.getUserId());
        }
        //转化
        List<WorkerCategory> workerCategories = workerCategoryConverter.params2pos(params);
        workerCategoryDao.insertCategories(workerCategories);
    }

    @Override
    public List<WorkerCategoryBO> getWorkerCategoriesByUserId(Long userId) {
        List<WorkerCategory> categories=workerCategoryDao.selectCategoriesByUserId(userId);
        //po bo
        return workerCategoryConverter.poList2BoList(categories);
    }

    @Override
    public void deleteWorkerCategory(Long userId) {
        workerCategoryDao.delete(userId);
    }
}
